package me.zhengjie.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.dto.MesMessage;
import me.zhengjie.dto.Resp;
import me.zhengjie.dto.sqlServer.Processcard;
import me.zhengjie.dto.sqlServer.ProductionInfo;
import me.zhengjie.dto.sqlServer.ProductionStation;
import me.zhengjie.domain.LayUpService;
import me.zhengjie.modbusTCP.LayUpTCP;
import me.zhengjie.repository.LayUpFlowToRepository;
import me.zhengjie.repository.ProcesscardRepository;
import me.zhengjie.repository.ProductionInfoRepository;
import me.zhengjie.repository.ProductionStationRepository;
import me.zhengjie.util.LoginUtil;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

@RestController
@RequiredArgsConstructor
@Api(tags = "叠合作业")
@RequestMapping("/api/layUp")
public class LayUpController {

    private final LayUpService layUpService;
    private final LayUpTCP layUpTCP;
    private final LayUpFlowToRepository layUpFlowToRepository;

    @Log("工位绑定工艺卡")
    @ApiOperation("工位绑定工艺卡")
    @GetMapping(value = "/bingingProcessCard/{processcardCode}/{stationCode}")
    public Resp bingingProcessCard(@PathVariable("processcardCode") String processcardCode,@PathVariable("stationCode") String stationCode){
        MesMessage mesMessage = layUpService.ScanProcesscard(processcardCode, stationCode);
//        System.out.println("工作站编码是+++"+stationCode);
        if (!ObjectUtil.isEmpty(mesMessage)){
            if (mesMessage.getSuccessCode()==200){
                return Resp.builder()
                        .status(200)
                        .data("mesMessage",mesMessage)
                        .build();
            }else {
                return Resp.builder()
                        .status(500)
                        .msg(mesMessage.getErrorMessage())
                        .build();
            }
        } else {
            return Resp.builder()
                    .status(500)
                    .msg("没有查找到工艺卡信息")
                    .build();
        }
    }

    @Log("打开叠法图")
    @ApiOperation("打开叠法图")
    @GetMapping("/openPicture/{workNum}")
    public ResponseEntity<InputStreamResource> openPicture(@PathVariable("workNum") String workNum){
        try {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.IMAGE_JPEG);
            FileInputStream fileInputStream = new FileInputStream("G:\\download\\wallhaven-yxk33g.jpg");
            InputStream inputStream = (InputStream) fileInputStream;
            InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
            return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Log("载盘绑定工艺卡")
    @ApiOperation("载盘绑定工艺卡")
    @GetMapping(value = "/bindingPallet/{palletCode}/{stationCode}/{flowTo}")
    public Resp bindingPallet(@PathVariable("palletCode") String palletCode,@PathVariable("stationCode") String stationCode,@PathVariable("flowTo") Integer flowTo){
        try {
            layUpService.scanPalletCode(flowTo,palletCode,stationCode);

            return Resp.builder()
                    .status(200)
                    .msg("绑定成功")
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.builder()
                    .status(500)
                    .msg("绑定失败")
                    .build();
        }

    }

    @Log("强制结束生产")
    @ApiOperation("强制结束生产")
    @GetMapping(value = "/shutdownLayUp/{palletCode}")
    public Resp shutdownLayUp(@PathVariable("palletCode") String palletCode){
        try {
            layUpService.forcefulFinish(palletCode);
            return Resp.builder()
                    .status(200)
                    .msg("已结束")
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            return Resp.builder()
                    .status(500)
                    .msg("强制结束失败")
                    .build();
        }
    }

    @Log("查找流向")
    @ApiOperation("查找流向")
    @GetMapping(value = "/allFlowInfo")
    public Resp findFlowInfo(){
        return Resp.builder()
                .data("flowMission",layUpFlowToRepository.findAll())
                .status(200)
                .build();
    }
}
